package com.liao.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.liao.mapper.GoodsMapper;
import com.liao.po.Goods;
import com.liao.service.GoodsService;
import com.liao.utils.PageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    private Integer page = 1;
    private Integer limit = 10;

    @Override
    public PageUtils queryPageByCodition(Map<String, Object> params) {


        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery();

        String pagekey = (String) params.get("page");
        if (!StringUtils.isEmpty(pagekey) && !"0".equalsIgnoreCase(pagekey)) {
            page = Integer.parseInt(pagekey);
        }

        String limitkey = (String) params.get("limit");
        if (!StringUtils.isEmpty(limitkey) && !"0".equalsIgnoreCase(limitkey)) {
            limit = Integer.parseInt(limitkey);
        }

        String searchKey = (String) params.get("key");
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.and(wq ->{
                 wq.eq(Goods::getGid, searchKey).or().like(Goods::getGname, searchKey).or().like(Goods::getAddress, searchKey);
            });
        }

        String priceString = (String) params.get("price");
        if (!priceString.isEmpty()) {

            Map price = JSON.parseObject(priceString, Map.class);

            Integer min = (Integer) price.get("min");
            if (min >= 0) {
                queryWrapper.ge(Goods::getPrice, min);
            }

            Integer max = (Integer) price.get("max");
//        String max = (String) params.get("max");

            if (max > 0) {
                try {
                    BigDecimal bigDecimal = new BigDecimal(max);
                    if (bigDecimal.compareTo(BigDecimal.ZERO) == 1) {
                        queryWrapper.le(Goods::getPrice, max);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        String publishStatuskey = (String) params.get("publishStatus");
        if (!StringUtils.isEmpty(publishStatuskey)) {
            int publishStatus = Integer.parseInt(publishStatuskey);
            if (publishStatus !=-1) {
                queryWrapper.eq(Goods::getPublishStatus,publishStatus);
            }

        }


        Page<Goods> goodsPage = new Page<>(page, limit);
        IPage<Goods> goodsIPage = this.baseMapper.selectPage(goodsPage, queryWrapper);
        System.out.println("总页数： " + goodsIPage.getPages());
        System.out.println("总记录数： " + goodsIPage.getTotal());
        goodsIPage.getRecords().forEach(System.out::println);
        return new PageUtils(goodsIPage);
    }


}
